import random
import KBEngine
import KBEDebug as log


class Avatar(KBEngine.Entity):

    def __init__(self):
        KBEngine.Entity.__init__(self)

        self.callbackID = 0
        self.localTime = 0

    def onAvatarEnabled(self):
        log.DEBUG_MSG('bots::Avatar.onAvatarEnabled')
        self.callbackID = KBEngine.callback(1, self.update)

    def onEnterSpace(self):
        log.DEBUG_MSG(f'bots::Avatar.onEnterSpace {self.id}')

    def onLeaveSpace(self):
        log.DEBUG_MSG(f'bots::Avatar.onLeaveSpace {self.id}')

    def onBecomePlayer(self):
        log.DEBUG_MSG(f'bots::Avatar.onBecomePlayer {self.id}')

    def onBecomeNonPlayer(self):
        self.DEBUG_MSG(f'bots::Avatar.onBecomeNonPlayer {self.id}')
        KBEngine.cancelCallback(self.callbackID)

    def onKillServer(self, s):
        log.INFO_MSG('onKillServer')

    def callbackMsgId(self, code):
        log.INFO_MSG('callbackMsgId {code}')

    def OnCreateNewAvatarSuccess(self):
        pass

    def OnSelectAvatar(self):
        pass

    def OnDelAvatarSuccess(self, arg):
        pass

    def repSeasonRank(self, arg1, arg2, arg3):
        pass

    def repGroupMemberInRank(self, arg1, arg2, arg3):
        pass

    def repNoGroup(self):
        pass

    def onLayEggs(self):
        pass

    def update(self):
        rd = random.randrange(1, 3)
        if rd == 1:
            self.farm.base.sellEgg(0)
        elif rd == 2:
            self.farm.base.sellChicken(0)
        else:
            self.avatarOther.base.startGame(1, 2)
            self.avatarOther.base.gameFinish(1, 1)

        self.callbackID = KBEngine.callback(rd, self.update)
    
    def repTaskInfo(self, arg1, arg2, arg3):
        pass